package com.nakamachizu.controller.mixi;

import java.util.Calendar;
import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;
import org.slim3.datastore.Datastore;
import org.slim3.util.TimeZoneLocator;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.nakamachizu.meta.MarkDataMeta;
import com.nakamachizu.model.LandMarkData;
import com.nakamachizu.model.MapData;
import com.nakamachizu.model.MarkData;
import com.nakamachizu.model.session.MixiSessionData;
import com.nakamachizu.service.MapDataService;
import com.nakamachizu.util.ErrorsUtil;
import com.nakamachizu.util.PocketCloudStringUtil;

public class EditController extends Controller {

    private static final Logger log = Logger.getLogger(EditController.class.getName());
    private MarkDataMeta meta = MarkDataMeta.get();
    
    @Override
    public Navigation run() throws Exception {

        String keyStr = requestScope("id");
        String sid = requestScope("sid");
        if(validate()){
            Calendar now = Calendar.getInstance(TimeZoneLocator.get());
            
            MapDataService mds = new MapDataService();
            MixiSessionData session = mds.getMixiSessionData(sid);
            
            Key key = KeyFactory.stringToKey(keyStr);
            MarkData md = Datastore.get(MarkData.class, key);
            MapData map = md.getMapDataRef().getModel();
            if(md != null && session.isOwner() && map.getAdminMailAddress().equals(session.getAdminMailAddress())){
                String name = requestScope("name");
                String comment = requestScope("comment");
                String icon = md.getPcIconName();
                if(LandMarkData.class.getSimpleName().equals(md.getMarkType())){
                    icon = requestScope("ico");
                }
                
                //String lat = requestScope("lat");
                //String lng = requestScope("lng");
                //GeoPt pt = new GeoPt(Float.parseFloat(lat), Float.parseFloat(lng));
                
                md.setName(PocketCloudStringUtil.convertWindows31J(name));
                md.setComment(PocketCloudStringUtil.convertWindows31J(comment));
                md.setPcIconName(icon);
                md.setModifyDate(now.getTime());
                
                Datastore.put(md);
            }
        }
        
        return redirect("/mixi/view?sid=" + sid);
    }
    
    protected boolean validate() {
        boolean result = false;
        Validators v = new Validators(request);
        v.add("id", v.required());
        v.add("sid", v.required());
        v.add(meta.name, v.required());
        v.add(meta.comment, v.required());
        v.add(meta.name, v.maxlength(10));
        v.add(meta.comment, v.maxlength(100));
        v.add("ico", v.required());
        
        result = v.validate();
        
        return result;
    }
    
    @Override
    protected Navigation handleError(Throwable error) {
        error.printStackTrace();
        log.severe(ErrorsUtil.stackTrace(error));
        return forward("/jsp/pc/mixi/error.jsp");
    }
}
